package org.ccay.excel.importer.service.impl.internal;

import javax.inject.Inject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

import org.ccay.core.exception.ApplicationException;
import org.ccay.excel.importer.service.IExcelDataConsumer;
import org.ccay.excel.importer.service.IExcelImportSupport;
import org.ccay.excel.meta.IExcelMetaManager;
import org.ccay.excel.meta.SheetMeta;
import org.ccay.excel.util.impl.BaseExcelSupport;

/**
 * IExcelImportSupport的默认实现。
 * 	
 * @author chaowangbang
 * @since Mar 16, 2012
 */
public class DefaultExcelImportSupport extends BaseExcelSupport implements IExcelImportSupport, ApplicationContextAware {
	
	/**
	 * 对应的资源
	 */
	private Resource resource;
	
	private ApplicationContext springContext;
	
	@Inject
	private IExcelMetaManager excelMetaManager;
	
	/** 初始化这个Bean.	
	 * @author chaowangbang
	 * @throws ApplicationException 
	 * @since Mar 16, 2012
	 */
	protected void initialize() throws ApplicationException {
		// 解析Resource，获取ExcelMeta
		setExcelMeta(excelMetaManager.getExcelMeta(resource));
		Assert.notNull(this.excelMeta);
	}
	
	/** 设置资源	
	 * @author chaowangbang
	 * @since Mar 16, 2012
	 * @param resource
	 */
	public void setResource(Resource resource) {
		this.resource = resource;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.springContext = applicationContext;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.importer.service.IExcelImportSupport#getExcelDataConsumer(java.lang.String)
	 */
	public IExcelDataConsumer getExcelDataConsumer(String sheetName) {
		SheetMeta sheetMeta = getSheetMeta(sheetName);
		String consumerBean = sheetMeta.getConsumerBean();
		return springContext.getBean(consumerBean, IExcelDataConsumer.class);
	}
}
